package com.itheima.quartz;

import com.itheima.arithmetic.ReducePriceUtils;
import com.itheima.dao.MoneyPackageDao;
import com.itheima.domain.MoneyPackage;
import com.itheima.util.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/*****
 * @Author: http://www.itheima.com
 * @Description: com.itheima.quartz.MoneyPushTask
 ****/
@Component
public class MoneyPushTask {
    @Autowired
    private MoneyPackageDao moneyPackageDao;
    @Autowired
    private RedisTemplate redisTemplate;

    /****
     * 每30秒加载一次红包信息到队列中
     */
    @Scheduled(cron = "0/5 * * * * ?") //定时任务Quartz
    public void pushMoney(){
        //1.查询要发的红包总金额信息
        MoneyPackage one = moneyPackageDao.findOne();

        if(one!=null){
            //2将红包总金额分层N份红包
            List<Double> doubles = ReducePriceUtils.splitReducePrice(one.getMoney(), one.getCount());

            //3.将分好的红包存入到Redis集合中List->队列
            Double[] moneys = new Double[doubles.size()];
            doubles.toArray(moneys);
            redisTemplate.boundListOps(Constant.MONEY_QUEUE_NAME).leftPushAll(moneys);
        }

    }
}
